﻿using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class J1939Collector : ModbusTcpDataCollector
    {
        const ushort startAddress = 0;    //起始地址
        const ushort numberOfPoints = 40; //读数据长度
        public J1939Collector(DataCollectionContext dataCollectionContext) : base(dataCollectionContext)
        {
        }

        protected async override Task CollectDataInternalAsync()
        {
            ushort[] buff = ModbusMaster.ReadHoldingRegisters(CollectorConfig.SlaveAddress, startAddress, numberOfPoints);
            if (buff.Length == numberOfPoints)
            {
                //故障码
                var data1 = (buff[0] & 0xff00) >> 8;
                SetAISensorValue(1, data1);

                //发动机转速
                var b4 = (byte)(buff[5] >> 8);
                var b5 = (byte)buff[6];
                var data2 = (b5 << 8 | b4) * 0.125f;
                SetAISensorValue(2, data2);

                //发动机冷却水温 摄氏度
                var data3 = (buff[8] & 0xff) - 40;
                SetAISensorValue(3, data3);

                //充电机电位 
                var data4 = buff[14] * 0.05f;
                SetAISensorValue(4, data4);

                //机油压力 
                var data5 = ((buff[17] & 0xff00) >> 8) * 4 * 0.01f;
                SetAISensorValue(5, data5);

                //负载
                var data6 = buff[21] & 0xff;
                SetAISensorValue(6, data6);

                //发动机小时
                var data7 = (buff[24] + buff[25]) / 20f;
                SetAISensorValue(7, data7);

                //尿素箱温度
                var data8 = ((buff[32] & 0xff00) >> 8) - 40;
                SetAISensorValue(8, data8);

                //尿素箱容积
                var data9 = (buff[32] & 0xff) * 0.4f;
                SetAISensorValue(9, data9);



                //DI量
                //通知操作员，后处理要求增加 00b = 无请求 01b = 所需的后处理发动机转速控制 10b = 所需的紧急后处理发动机转速控制 11b = 不可用 指示后处理是否希望发动机负载为后处理为热目的而要求发动机负载。

                if (CollectorConfig.DICount >= 1)
                {
                    //两位，第一字节，第五第六位
                    int a = (buff[28] & 0xff00) >> 10;
                    bool value = false;
                    switch (a)
                    {
                        case 0:
                            value = false;
                            break;
                        case 1:
                            value = true;
                            break;
                        case 2:
                            value = true;
                            break;
                        case 3:
                            break;
                        default:
                            break;
                    }
                    SetDICoilValue(1,value);
                }
            }
            else
            {

            }
            //can转Modbus模块在CAN关闭时会保留最后值，此处清理数据
            //因为这行代码会清零数据，所以会导致读到的数据一会是0，一会是正常值。找另外的时机加入这行代码。
            //master.WriteMultipleRegisters(Collector.SlaveAddress, startAddress, new ushort[40]);
        }

    }
}
